package view;

import java.util.ArrayList;
import java.util.Scanner;

import service.Constan;
import service.TimesheetAPI;
import service.implement.TimesheetDAO;
import model.Timesheet;

public class TimesheetApp {
	Timesheet tst=new Timesheet();
	TimesheetAPI ts=new TimesheetDAO();
	Scanner sc=new Scanner(System.in);
	public void menuCapnhatTs(){
		System.out.println("1.Cap nhat theo id");
		System.out.println("2.Cap nhat theo employeeid");
		System.out.println("3.Cap nhat theo projectid");
		System.out.println("4.Thoat");
	}
	public void menuDeleteTs(){
		System.out.println("1.Xoa Timesheet theo id");
		System.out.println("2.Xoa Timesheet theo employeeid");
		System.out.println("3.Xoa Timesheet theo projectid");
		System.out.println("4.Thoat");
	}
	public void menuTimkiemTs(){
		System.out.println("1.Tim kiem theo Id");
		System.out.println("2.Tim kiem theo employeeid");
		System.out.println("3.Tim kiem theo projectid");
		System.out.println("4.Thoat");
		
	}
	public void capNhattsbyid(){
		try{
			if(ts.updateTimesheet(tst.updateTimesheet())==Constan.SER_STATUS_EROOR){
				System.out.println("Not Update successfull");
			}
			else{
				System.out.println("Update Successfull");
			}
		}
		catch(NullPointerException e){
			System.out.println(" Vui long thu lai!");
		}
	}
	public void capNhattsbyEmpid(){
		System.out.println("Nhap employeeid can update");
		int employeeid=Integer.parseInt(sc.nextLine());
		if(ts.getTimesheetByEmployeeid(employeeid).isEmpty()){
			System.out.println("Khong ton tai Employeeid");
		}else{
			System.out.println(Constan.DISPLAY_TS);
			ArrayList<Timesheet> timesheet=ts.getTimesheetByEmployeeid(employeeid);
			for(Timesheet a:timesheet){
				System.out.println(a);
			}
			capNhattsbyid();
		}
	}
	public void capNhattsbyPjId(){
		System.out.println("Nhap projectid can update");
		int projectid=Integer.parseInt(sc.nextLine());
		if(ts.getTimesheetByProjectid(projectid).isEmpty()){
			System.out.println("Khong ton tai Projectid");
		}else{
			System.out.println(Constan.DISPLAY_TS);
			ArrayList<Timesheet> timesheet=ts.getTimesheetByProjectid(projectid);
			for(Timesheet a:timesheet){
				System.out.println(a);
			}
			capNhattsbyid();
		}
	}
	public void inTs(){
		ArrayList<Timesheet> tst=ts.getTimesheet();
		System.out.println("===================================================================Danh sach Timesheet============================================================\n");
		System.out.println(Constan.DISPLAY_TS);
		for(Timesheet t:tst){
			System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
			System.out.println(t);
		}
	}
	public void nhapTs(){
		try{
			if(ts.addTimesheet(tst.inputTimesheet())==Constan.SER_STATUS_EROOR){
				System.out.println("Not Add successfull");
			}
			else{
				System.out.println("Add Successfull");
			}

		}catch(NullPointerException e){
			System.out.println("Vui long thu lai");
		}
	}
	public void deleteTsbyId(){
		System.out.println("Nhap id can xoa: ");
		int id=Integer.parseInt(sc.nextLine());
		if(ts.getTimesheetById(id)==null){
			System.out.println("Id khong ton tai");
		}else{
			System.out.println("Thong tin id da co");
			Timesheet show=ts.getTimesheetById(id);
			System.out.println(Constan.DISPLAY_TS);
			System.out.println(show);
			System.out.println("Ban thuc su muon xoa");
			System.out.println("1.Co");
			System.out.println("2.Khong");
			int chon=Integer.parseInt(sc.nextLine());
			if(chon==1){
				if(ts.deleteTimesheet(id)==Constan.SER_STATUS_SUCSESS){
					System.out.println("Delete Successfull");
				}
				else
					System.out.println("Not Delete Successful");
			}
			else System.out.println("Xoa khong thanh cong.Thu lai");
		}
	}
	public void deleteTsByEmpId(){
		System.out.println("Nhap Employeeid can xoa: ");
		int employeeid=Integer.parseInt(sc.nextLine());
		if(ts.getTimesheetByEmployeeid(employeeid).isEmpty()){
			System.out.println("Employeeid khong ton tai");
		}else{
			System.out.println("Thong tin Employeeid da co:");
			System.out.println(Constan.DISPLAY_TS);
			ArrayList<Timesheet> timesheet=ts.getTimesheetByEmployeeid(employeeid);
			for(Timesheet t:timesheet){
				System.out.println(t);
			}
			deleteTsbyId();
		}
	}
	public void deleteTsByPjId(){
		System.out.println("Nhap Projectid can xoa: ");
		int projectid=Integer.parseInt(sc.nextLine());
		if(ts.getTimesheetByProjectid(projectid).isEmpty()){
			System.out.println("Projectid khong ton tai");
		}else{
			System.out.println("Thong tin projectid da co: ");
			System.out.println(Constan.DISPLAY_TS);
			ArrayList<Timesheet> timesheet=ts.getTimesheetByProjectid(projectid);
			for(Timesheet t:timesheet){
				System.out.println(t);
			}
			deleteTsbyId();
		}
	}
	public void searchTsByid(){
		System.out.println("Nhap id can tim kiem");
		int id=Integer.parseInt(sc.nextLine());
		if(ts.getTimesheetById(id)==null){
			System.out.println("Khong ton tai id");
		}
		else{
			Timesheet show=ts.getTimesheetById(id);
			System.out.println(Constan.DISPLAY_TS);
			System.out.println(show);
		}
	}
	public void searchTsByEmpId(){
		System.out.println("Nhap employeeid can tim kiem");
		int employeeid=Integer.parseInt(sc.nextLine());
		if(ts.getTimesheetByEmployeeid(employeeid).isEmpty()){
			System.out.println("Employeeid khong ton tai");
		}else{
			System.out.println(Constan.DISPLAY_TS);
			ArrayList<Timesheet> timesheet=ts.getTimesheetByEmployeeid(employeeid);
			for(Timesheet t:timesheet){
				System.out.println(t);
			}
		}
	}
	public void searchTsByPjId(){
		System.out.println("Nhap projectid can tim kiem");
		int projectid=Integer.parseInt(sc.nextLine());
		if(ts.getTimesheetByProjectid(projectid).isEmpty()){
			System.out.println("Khong ton tai Projectid");
		}else{
			System.out.println(Constan.DISPLAY_TS);
			ArrayList<Timesheet> timesheet=ts.getTimesheetByProjectid(projectid);
			for(Timesheet t:timesheet){
				System.out.println(t);
			}
		}
	}

}
